/**
 * 
 */
package com.shiftl.ui.aroma;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * Table constructed with titles Styles: tableWithTitles{} - main style
 * 
 * @author alexmalyovanny
 */
public class TableWithTitles extends FlexTable {
	private String[] storedTitles;

	public TableWithTitles(final String... titles) {
		storedTitles = titles;
		setStyleName("tableWithTitles");
		getRowFormatter().setStyleName(0, "tableWithTitlesHeader");

		for (int col = 0; col < titles.length; col++) {
			setText(0, col, titles[col]);
		}

	}

	public int getTitleCount() {
		return getCellCount(0);
	}

	public Map<String, String> getValues(final int rowNum) {
		Map<String, String> valueMap = new HashMap<String, String>();
		TextBox tmpBox;
		for (int col = 0; col < getTitleCount(); col++) {
			tmpBox = (TextBox) getWidget(rowNum, col);
			if (tmpBox != null) {
				valueMap.put(getText(0, col), tmpBox.getText());
			}
		}
		return valueMap;
	}

	//
	// public void addTextRow(final String... strings) {
	// int nextRow = getRowCount();
	// getRowFormatter().setStyleName(nextRow, "tableWithTitlesBrowseRow");
	// for (int i = 0; i < strings.length; i++) {
	// setText(nextRow, i, strings[i]);
	// }
	// }
	//
	// public void addWidgetRow(final Widget... widgets) {
	// int nextRow = getRowCount();
	// for (int i = 0; i < widgets.length; i++) {
	// setWidget(nextRow, i, widgets[i]);
	// }
	// }

	public void fillRowWithTextMap(final Map<String, String> map, final int rowNum) {
		for (int col = 0; col < storedTitles.length; col++) {
			setText(rowNum, col, map.get(storedTitles[col]));
		}
	}

	public void fillRowWithWidgetMap(final Map<String, Widget> map, final int rowNum) {
		for (int col = 0; col < storedTitles.length; col++) {
			setWidget(rowNum, col, map.get(storedTitles[col]));
		}
	}
}
